package com.itheima.leyou.service;

import com.itheima.leyou.dao.UserDao;
import com.sun.corba.se.spi.ior.ObjectKey;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserService {

    @Resource
    UserDao userDao;

    public Map<String, Object> getUser(String username, String password){
        Map<String, Object> resultMap = new HashMap<String, Object>();
        if (username==null||username.equals("")){
            resultMap.put("result", false);
            resultMap.put("msg", "传入参数有误！");
            return resultMap;
        }

        ArrayList<Map<String, Object>> list = userDao.getUser(username, password);

        if (list==null||list.isEmpty()){
            resultMap.put("result", false);
            resultMap.put("msg", "没有取出会员数据！");
            return resultMap;
        }

        resultMap = list.get(0);
        resultMap.put("result", true);
        resultMap.put("msg", "");
        return resultMap;
    }


    public Map<String, Object> insertUser(String username, String password){
        Map<String, Object> resultMap = new HashMap<String, Object>();
        if (username==null||username.equals("")){
            resultMap.put("result", false);
            resultMap.put("msg", "传入参数有误！");
            return resultMap;
        }

        int new_id = 0;
        new_id = userDao.insertUser(username, password);
        if (new_id <= 0){
            resultMap.put("result", false);
            resultMap.put("msg", "写入会员失败！");
            return resultMap;
        }

        resultMap.put("username", username);
        resultMap.put("password", password);
        resultMap.put("id", new_id);
        resultMap.put("phone", username);
        resultMap.put("result", true);
        resultMap.put("msg", "");
        return resultMap;
    }
}
